<?php
class ControllerDelimanDanhsachkhachhangmanhanvienphathanhbaophutrach extends Controller
{
	private $error = array();
   	function __construct() 
	{
		$listchek = $this->getListCheck();
		if(in_array($this->getMethod(),$listchek))
		{
			if(!$this->user->hasPermission($this->getRoute(), $this->getMethod()))
			{
				$this->response->redirect("?route=common/permission");
			}
		}
				
	 	$this->load->model("deliman/khachhang");
		$this->load->model("common/control");
		$this->load->model("deliman/khuvuc");
		$this->load->model("deliman/nhanvien");
		
   	}
	
	public function index()
	{
		$this->getList();	
	}
	
	public function getnhanvien()
	{		
		$khuvucid = $this->request->get['khuvucid'];
		//$select =  $this->request->get['select'];
		$khuvus = array();
		$this->model_deliman_khuvuc->getTree($khuvucid, $khuvus);
		$arrkhuvucid = $this->string->matrixToArray($khuvus,'khuvucid');
		$arrkhuvucid = array_unique($arrkhuvucid);
		$where = " AND khuvucid in ('".implode("','",$arrkhuvucid)."')";
		
		$phutrackhuvucs = $this->model_deliman_nhanvien->getPhuTrachKhuVucList($where." AND tinhtrang <>'deleted'");
		$listnhanvienphutrachid = $this->string->matrixToArray($phutrackhuvucs,'nhanvienphutrachid');
		$listnhanvienphutrachid = array_unique($listnhanvienphutrachid);
		
		$where = " AND nhanvienid in ('".implode("','",$listnhanvienphutrachid)."')";
		$where .= " AND loainhanvienid = 3 ";
		$nhanviens = $this->model_deliman_nhanvien->getList($where);
		$this->data['output'] = $this->model_common_control->getDataCombobox($nhanviens, "hoten", "nhanvienid", $select);
		
		$this->template='common/output.tpl';

		$this->render();
	}
	
	//Mặc định 
	private function getList() 
	{	
		//lấy thông tin đặt báo có điều kiện		
		$data = $this->request->get;
		$arrKhachHang=array();
		$this->data['dskhuvuc']=array();
		$this->data['datas']=array();
		$this->data['loainhanvienid']='';
		
		//Lấy ds khu vực quản lý
		// Lay ds khuvucid của các khu vực mà nhân viên đang đăng nhập quản lý
		$dskhuvucid = array();
		
		//Nếu là nhân viên phát báo thì load lên luôn
		if(trim($this->user->getNhanVien('loainhanvienid')) == '3')
		{
			$data['nhanvienid'] = $this->user->getNhanVien('nhanvienid');
			$this->data['loainhanvienid'] = $this->user->getNhanVien('loainhanvienid');
		}
		else
		{
			if(trim($this->user->getNhanVien('loainhanvienid')) == '2')
			{
				$dskhuvucid = $this->model_deliman_nhanvien->getPhuTrachKhuVucList(" AND nhanvienphutrachid ='".$this->user->getNhanVien('nhanvienid')."' AND tinhtrang != 'deleted' ORDER BY khuvucid ASC");
				$dskhuvucid = $this->string->matrixToArray($dskhuvucid, 'khuvucid');
				$dskhuvucid = array_unique($dskhuvucid);
			}
			else
			{
				$dskhuvucid[]='0';
			}
		}
		
		//Danh sach khu vuc	theo TREE
		foreach($dskhuvucid as $khuvucid)
		{
			$dskv=array();
			$this->model_deliman_khuvuc->getTree($khuvucid,$dskv,$khuvucid);
			$this->data['dskhuvuc'] = array_merge($this->data['dskhuvuc'], $dskv);
		}
		
		$where = "";
		/*if (trim($data['tungay']) != "")
		{
			$where .= " AND ngayphutrach >='".$this->date->formatViewDate($data['tungay'])."'";
		}
		if (trim($data['denngay']) != "")
		{
			$where .= " AND ngayphutrach <= '".$this->date->formatViewDate($data['denngay'])." 24:00:00' ";
		}*/
		if($data['nhanvienid'] != "")
		{
			$where .= " AND nhanvienphutrachid = '".$data['nhanvienid']."'";			
			$arrKhachHang=$this->model_deliman_khachhang->getList($where);			
		}
		else
		{
			$khuvus = array();
			if($data['khuvucid']!="")
				$this->model_deliman_khuvuc->getTree($data['khuvucid'], $khuvus);
			else
				$khuvus = $this->data['dskhuvuc'];
			$arrkhuvucid = $this->string->matrixToArray($khuvus,'khuvucid');
			$arrkhuvucid = array_unique($arrkhuvucid);
			$where = " AND khuvucid in ('".implode("','",$arrkhuvucid)."')";
			
			$phutrackhuvucs = $this->model_deliman_nhanvien->getPhuTrachKhuVucList($where." AND tinhtrang<>'deleted'");
			$listnhanvienphutrachid = $this->string->matrixToArray($phutrackhuvucs,'nhanvienphutrachid');
			$listnhanvienphutrachid = array_unique($listnhanvienphutrachid);
			
			
			$where = " AND nhanvienid in ('".implode("','",$listnhanvienphutrachid)."')";
			$where .= " AND loainhanvienid = 3 AND trangthai <>'deleted'";
			$nhanviens = $this->model_deliman_nhanvien->getList($where);
			$arr_nhanvienid = $this->string->matrixToArray($nhanviens,'nhanvienid');
			
			$arr_nhanvienid = array_unique($arr_nhanvienid);
			
			//Lấy ds khách hàng cần xem
			$where = " AND nhanvienphutrachid in ('".implode("','",$arr_nhanvienid)."')";
			$arrKhachHang=$this->model_deliman_khachhang->getList($where);	
		}

		for($i=0;$i<count($arrKhachHang);$i++)

		{
			$this->data['datas'][$i]['hoten'] = $arrKhachHang[$i]['hoten'];
			$this->data['datas'][$i]['diachi'] = $arrKhachHang[$i]['diachi'];
			$this->data['datas'][$i]['email'] = $arrKhachHang[$i]['email'];
			$this->data['datas'][$i]['dienthoai'] = $arrKhachHang[$i]['dienthoai'];
			$nv = $nhanviens = $this->model_deliman_nhanvien->getItem($arrKhachHang[$i]['nhanvienphutrachid']);
			$this->data['datas'][$i]['tennhanvienphutrach'] = $nv['hoten'];
		}
				
		//print_r($this->data['datas']);
		$this->data['refres']=$_SERVER['QUERY_STRING'];
		$this->id='content';
		$this->template="deliman/danhsachkhachhangmanhanvienphathanhbaophutrach.tpl";
		
		if($data['print'] == "true")
			$this->layout="layout/print";
		else
			$this->layout="layout/deliman";
		
		$this->render();
	}
}
?>